package com.example.androidtest3

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class FourthActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_fourth)
        val button2=findViewById<Button>(R.id.button2)
        button2.setOnClickListener {
                // 3. 已获取权限，直接打开相机
                disPathTakePictureIntent()
        }
        val finish=findViewById<Button>(R.id.finish)
        finish.setOnClickListener {
            finish()
        }
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        val imageView3=findViewById<ImageView>(R.id.imageView3)
        if(requestCode==1&&resultCode==RESULT_OK){
            val imageBitmap=data?.extras?.get("data")as Bitmap
            imageBitmap.let{
                imageView3.setImageBitmap(it)
                Toast.makeText(this, "拍照失败", Toast.LENGTH_SHORT).show()
            }
        }
    }

    private fun disPathTakePictureIntent() {
        val pictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        if (intent.resolveActivity(packageManager) != null) {
            startActivityForResult(pictureIntent, 1)
        }
    }
}